package com.stock.stockana.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.stock.publiclib.utils.StringUtil;
import com.stock.stockana.R;
import com.stock.stockana.StockCenter;
import com.stock.stockana.beans.StockInfo;
import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.requests.GetAllStocks;
import com.stock.stockana.requests.GetRecStocks;
import com.stock.stockana.ui.activities.MainActivity;
import com.stock.stockana.ui.custom.QNumberPicker;
import com.stock.stockana.ui.listener.OnStockItemClickedListener;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * ---------------------------------------------------
 * Description: 推荐股票页面,股票列表适配器
 * Author: ljgsonx
 * Belong to: com.stock.stockana.ui.adapter
 * Time: 2016/1/26 16:29
 * ---------------------------------------------------
 */
public class CollectStockAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private LinearLayout noItemTipLL;

    private final static int VIEWTYPE_TITLE = 0;
    private final static int VIEWTYPE_CONTENT = 1;

    public static List<StockInfoBean> mCollectStocks;
    public static boolean isSetted = true;

    private OnStockItemClickedListener mOnStockItemClickedListener;

    public void setmOnStockItemClickedListener(OnStockItemClickedListener onStockItemClickedListener){
        this.mOnStockItemClickedListener = onStockItemClickedListener;
    }

    public CollectStockAdapter(Context context) {
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(mContext);
        if(null == mCollectStocks) mCollectStocks = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case VIEWTYPE_TITLE:
                View view = mLayoutInflater.inflate(R.layout.collect_stock_title, parent, false);
                noItemTipLL = (LinearLayout) view.findViewById(R.id.noItemTipLL);
                holder = new TitleHolder(view);
                break;
            case VIEWTYPE_CONTENT:
                holder = new StocksHolder(mLayoutInflater.inflate(R.layout.collect_stock_item, parent, false));
                break;
            default:
                break;
        }

        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEWTYPE_TITLE;
        }else{
            return VIEWTYPE_CONTENT;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position != 0) {
            configViews((StocksHolder)holder,position-1);
            if(null != mOnStockItemClickedListener){
                ((RippleView)holder.itemView.findViewById(R.id.stockItemRv)).setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                    @Override
                    public void onComplete(RippleView rippleView) {
                        String stockNum = mCollectStocks.get(position - 1).getStNum();
                        for (StockInfoBean sb : GetAllStocks.stocks) {
                            if (!StringUtil.isBlank(stockNum)) {
                                if (stockNum.equals(sb.getStNum())) {
                                    mOnStockItemClickedListener.onStockItemCliked(sb);
                                    break;
                                }
                            }
                        }
                    }
                });
            }

        }else{
            if (mCollectStocks.size() == 0) {
                noItemTipLL.setVisibility(View.VISIBLE);
                TextView tipTv = (TextView) noItemTipLL.findViewById(R.id.list_tip_txt);
                if(isSetted){
                    tipTv.setText("您的自选股列表中没有已设置提醒的股票");
                }else{
                    tipTv.setText("您的自选股列表中没有未设置提醒的股票");
                }
            }else{
                noItemTipLL.setVisibility(View.GONE);
            }
        }
    }

    private void configViews(StocksHolder holder,int position) {
        holder.stockNameTv.setText(mCollectStocks.get(position).getStName());
        holder.stockNumTv.setText(mCollectStocks.get(position).getStNum());
        DecimalFormat df = new DecimalFormat("0.000");
        String price = df.format(mCollectStocks.get(position).getStPrice());
        holder.priceTv.setText(price);
        df = new DecimalFormat("0.00");
        String buyWakeUp = df.format(mCollectStocks.get(position).getDowntriggerprice());
        String saleWakeUp = df.format(mCollectStocks.get(position).getUptriggerprice());
        holder.buyWakeUpTv.setText(buyWakeUp);
        holder.saleWakeUpTv.setText(saleWakeUp);
        String remindStatus = mCollectStocks.get(position).getRemindStatus();
        if(!StringUtil.isBlank(remindStatus) && remindStatus.length() == 2){
            char buyStatus = remindStatus.charAt(0);
            char saleStatus = remindStatus.charAt(1);
            if (buyStatus == '0') {
                holder.buyWakeUpSwitchTv.setText("(关):");
            }else{
                holder.buyWakeUpSwitchTv.setText("(开):");
            }
            if (saleStatus == '0') {
                holder.saleWakeUpSwitchTv.setText("(关):");
            }else{
                holder.saleWakeUpSwitchTv.setText("(开):");
            }
        }
    }

    @Override
    public int getItemCount() {
        return 1 + mCollectStocks.size();
    }

    public static void updateSettedStocks() {
        if(mCollectStocks == null) return;
        mCollectStocks.clear();
        for (StockInfoBean sb : GetAllStocks.stocks) {
            if (sb.getDowntriggerprice() != 0 || sb.getUptriggerprice() != 0) {
                if(isSetted) mCollectStocks.add(sb);
            }else{
                if(!isSetted) mCollectStocks.add(sb);
            }
        }
    }

    class TitleHolder extends RecyclerView.ViewHolder {

        public TitleHolder(View itemView) {
            super(itemView);
        }
    }

    class StocksHolder extends RecyclerView.ViewHolder{
        public TextView stockNumTv;
        public TextView stockNameTv;
        public TextView priceTv;
        public TextView buyWakeUpTv;
        public TextView saleWakeUpTv;
        public TextView buyWakeUpSwitchTv;
        public TextView saleWakeUpSwitchTv;

        public StocksHolder(View view) {
            super(view);
            this.stockNumTv = (TextView) view.findViewById(R.id.stockNumTv);
            this.stockNameTv = (TextView) view.findViewById(R.id.stockNameTv);
            this.priceTv = (TextView) view.findViewById(R.id.stockPriceTv);
            this.buyWakeUpTv = (TextView) view.findViewById(R.id.buyWakeUpTv);
            this.saleWakeUpTv = (TextView) view.findViewById(R.id.saleWakeUpTv);
            this.buyWakeUpSwitchTv = (TextView) view.findViewById(R.id.buyWakeUpSwithTv);
            this.saleWakeUpSwitchTv = (TextView) view.findViewById(R.id.saleWakeUpSwithTv);
        }
    }

}
